<?php

    class SessionManager {
    
        //static private $instance = NULL;
    
        private function __construct() {
        }    
    
        public static function startSession() {
            session_start();
        }    
        
        public static function loginUser($userID) {
            if (self::setUserReady($userID)) {
                $_SESSION['userId'] = $userID;
                return true;
            } else return false;
        }
        
        public static function prepareUser($userID) {
            // unset($_SESSION['userId']);
            $_SESSION['puserId'] = $userID;
            // echo "prepared user ".$userID.", session var is: ".$_SESSION['puserId']."\n";
        }
        
        public static function getPreparedUserId() {
            return $_SESSION['puserId'];
        }
        
        public static function isUserPrepared($userID=null) {
            return (($userID == null) ? isset($_SESSION['puserId']) : ($_SESSION['puserId'] == $userID));
        }
        
        private static function setUserReady($userID) {
            if (self::isUserPrepared($userID)) {
                unset($_SESSION['puserId']);
                return true;
            } else return false;
        } 
        
        public static function getCurrentUserID() {
            return $_SESSION['userId'];
        }
        
        public static function isUserLoggedIn() {
            return isset($_SESSION['userId']);
        }
        
        public static function logoutUser() {
             unset($_SESSION['userId']);
        }
        
        public static function setRegistrationRequired() {
            $_SESSION['rreq'] = true;
        }
        
        public static function isRegistrationRequired() {
            return (isset($_SESSION['rreq']) && ($_SESSION['rreq'] == true));
        }
        
        public static function registerUser($userId) {
            if (self::isUserLoggedIn() && (self::getCurrentUserID() == $userId)) {
                $_SESSION['ruserId'] = $userId;
                return true;
            } else return false;
        }
        
        public static function clearRegistration() {
            unset($_SESSION['ruserId']);
            unset($_SESSION['cuserId']);
        }
        
        public static function isUserRegistered($userId) {
            return (isset($_SESSION['ruserId']) && ($_SESSION['ruserId'] == $userId));
        } 
        
        public static function setUserChecked($userId) {
            $_SESSION['cuserId'] = $userId;
        }
        
        public static function isUserChecked($userId) {
            return (isset($_SESSION['cuserId']) && ($_SESSION['cuserId'] == $userId)); 
        }        
        
        public static function clearSessionData($sessVars=null) {
            unset($_SESSION['rreq']);        
            self::clearRegistration();
            self::logoutUser();            
            unset($_SESSION['puserId']);
            if ($sessVars != null) {
                foreach($sessVars as $sessVarName) {
                    unset($_SESSION[$sessVarName]);
                }
            }
        }       
        
        public static function registerVar($varId, $varValue) {
            $_SESSION[$varId] = $varValue;
        }
        
        public static function isVarSet($varId) {
            return isset($_SESSION[$varId]);
        }
        
        public static function getVar($varId) {
            return $_SESSION[$varId];
        }        
        
        public static function clearVar($varId) {
             unset($_SESSION[$varId]);
        }        
        
        public static function destroySession() {
            session_destroy();
        }
        
        /* public static function getInstance()
        {
            if (self::$instance == NULL) {
                self::$instance = new SessionManager();
            }
            return self::$instance;
        } */
        
        private function __clone() {
        }               
        
    }
    
    SessionManager::startSession();

?>
